const formatTime = (date) => {
  let dateObj = new Date(date * 1000);
  let year = dateObj.getFullYear();
  let month = ("0" + (dateObj.getMonth() + 1)).slice(-2);
  let day = ("0" + dateObj.getDate()).slice(-2);
  let hours = ("0" + dateObj.getHours()).slice(-2);
  let minutes = ("0" + dateObj.getMinutes()).slice(-2);
  let seconds = ("0" + dateObj.getSeconds()).slice(-2);
  
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

const wxGetAddress = (longitude, latitude) => {
  const amapFile = require('../config/amap-wx.130.js')
  const myAmapFun = new amapFile.AMapWX({
    key: 'd4cc6b30149bcccee7b6b5a40c628369'
  }); //创建一个实例化对象
  return new Promise((resolve, reject) => {
    myAmapFun.getRegeo({
      location: `${longitude},${latitude}`,
      success: (res) => {
        resolve(res[0])
      },
      fail: (err) => {
        resolve(null)
      }
    })
  })
}

module.exports = {
  formatTime,
  wxGetAddress
}